package ru.reaperoq.dto

import kotlinx.serialization.Serializable
import ru.reaperoq.database.Database
import ru.reaperoq.schema.Computer
import ru.reaperoq.schema.Location

@Serializable
data class ComputerResponse(
    val id: Int,
    val serialNumber: String,
    val model: String,
    val manufacturer: String,
    val purchaseDate: Long,
    val location: Location?,
    val status: String,
    val notes: String,
    val createdAt: Long,
    val createdBy: UserResponse,
    val verifiedBy: UserResponse,
    val lastVerifiedAt: Long,
    val components: List<ComponentResponse>
) {
    companion object {
        suspend fun from(computer: Computer, createdBy: UserResponse, verifiedBy: UserResponse, components: List<ComponentResponse>): ComputerResponse {
            return ComputerResponse(
                computer.id,
                computer.serialNumber,
                computer.model,
                computer.manufacturer,
                computer.purchaseDate,
                computer.locationId.let { Database.locationsService.read(it) },
                computer.status.toString(),
                computer.notes ?: "",
                computer.createdAt,
                createdBy,
                verifiedBy,
                computer.lastVerifiedAt,
                components
            )
        }
    }
}
